package com.quanyan.ic;

import com.alibaba.druid.pool.DruidDataSource;
import com.quanyan.ic.common.spring.SpringUtil;
import com.quanyan.ic.protocol.server.LightServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import javax.sql.DataSource;

@SpringBootApplication
@ComponentScan(basePackages = {
        "com.quanyan.ic.*"
})
public class Application {

    private static Logger logger = LoggerFactory.getLogger(Application.class);

    @Value("${tcp.server.port}")
    private Integer tcpServerPort;

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);    //启动web容器，HTTP 9081端口
        SpringUtil.getBean(LightServer.class).start();     //启动tcp服务端，TCP 9262端口
    }

    @Bean
    public LightServer lightServer(){
        LightServer lightServer = new LightServer();
        lightServer.setPort(tcpServerPort);
        return lightServer;
    }

    @Bean(name = "dataSource", destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return new DruidDataSource();
    }
}
